<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\User;
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $users = DB::table('users')->where('del','正常')->paginate(4);
        $count = DB::table('users')->count();
        return view('admin..user.user_index')->with('users',$users)->with('count',$count);
        

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //添加用户
        return view('admin.user.user_add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $user['sex']=$request->sex;
        $user['name']=$request->name;
        $user['email']=$request->email;
        $user['mobile']=$request->mobile;
        $user['password']=bcrypt($request->password);
        $user['created_at']=date('Y-m-d H:i:s');
        $user['updated_at']=date('Y-m-d H:i:s');
        DB::table('users')->insert($user);
        return redirect('admin/user');
        
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //显示单页详情
        $user = DB::table('users')->where('id',$id)->first();
        //$userInfo = DB::table('user_infos')->where('uid',$id)->first();

        return view('admin.user.user_show')->with('user',$user);
                //->with('userInfo',$userInfo);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $user = User::find($id);
        return view('admin.user.user_edit')->with('user',$user);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $user['sex']=$request->sex;
        $user['name']=$request->name;
        $user['email']=$request->email;
        $user['mobile']=$request->mobile;
        $user['password']=bcrypt($request->password);
        
        $user['updated_at']=date('Y-m-d H:i:s');
        DB::table('users')->where('id',$id)->update($user);
        return redirect('admin/user');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //删除
        // if(DB::table('users')->where('id',$id)->delete()){
        //     $data = 1;
        // }{
        //     $data = 0;
        // }


    }
}
